package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.OptionType;
import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyCategoryDictQuery;
import com.zl.tby.model.TbyCameraInfo;
import com.zl.tby.model.TbyCategoryDict;
import com.zl.tby.repository.TbyCategoryDictRepo;
import com.zl.tby.service.ITbyCategoryDictService;
import com.zl.tby.vo.TbyCategoryDictBean;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多级分类字典
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyCategoryDictServiceImpl implements ITbyCategoryDictService {

    @Db
    private TbyCategoryDictRepo tbyCategoryDictRepo;

    @Override
    @Tran
    public R<TbyCategoryDict> add(TbyCategoryDict entity) {
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyCategoryDictRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyCategoryDict> update(TbyCategoryDict entity) {
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyCategoryDictRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        if (this.tbyCategoryDictRepo.selectCount(mp -> mp.whereTrue().andEq("parent_id", id)) > 0) {
            return R.failed("存在子分类无法删除");
        }
        return tbyCategoryDictRepo.deleteById(id) == 1 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyCategoryDict get(Long id) {
        return tbyCategoryDictRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyCategoryDictBean> treeTableList(TbyCategoryDictQuery query) {
        List<TbyCategoryDictBean> resultList = TbyCategoryDictBean.recurData(0L, this.tbyCategoryDictRepo.selectList(mp -> {
            mp.whereTrue().andIf(StringUtils.isNotEmpty(query.getCategory()), " category = ?", query.getCategory());
        }));
        return new TbyPageResult(resultList, new BigDecimal(resultList.size()).longValue(), 1, 1000);
    }

    @Override
    public List<OptionType> options(TbyCategoryDictQuery query) {
        return TbyCategoryDictBean.recurRouteOptions(0L, this.tbyCategoryDictRepo.selectList(mp -> {
            mp.whereTrue().andIf(StringUtils.isNotEmpty(query.getCategory()), " category = ?", query.getCategory());
        }));
    }

    @Override
    public Map<Long, TbyCategoryDict> getIdMapByCategory(String category) {
        Map<Long, TbyCategoryDict> map = new HashMap<>(0);
        if (StringUtils.isEmpty(category)) {
            return map;
        }
        List<TbyCategoryDict> dbList = this.tbyCategoryDictRepo.selectList(mp -> { mp.whereEq("category", category); });
        for (TbyCategoryDict entity : dbList) {
            map.put(entity.getId(), entity);
        }
        return map;
    }

    @Override
    public R<Boolean> checkCode(Long id, String code) {
        if (StringUtils.isEmpty(code)) {
            return R.ok(false);
        }
        List<TbyCategoryDict> dbList = this.tbyCategoryDictRepo.selectList(mp -> {
            mp.whereEq("code", code);
        });
        for (TbyCategoryDict data : dbList) {
            if (id == null && data.getCode().equals(code)) {
                return R.ok(true);
            } else if (id != null && data.getCode().equals(code) && id.compareTo(data.getId()) != 0) {
                return R.ok(true);
            }
        }
        return R.ok(false);
    }
}
